<template>
	<div>
		<!--修改界面-->
		<el-dialog v-loading.fullscreen.lock="loading" element-loading-text="处理中......" title="修改特约 " :visible.sync="dialogVisible" size="small" :close-on-click-modal="false" :close-on-press-escape="false" :before-close="off" :top="dialog_margin_top">
			  <el-form label-width="160px" >
			  <el-row :span="24">
			  	<el-col :span="12">
			  		<el-form-item label="特约代码" required>
						<el-input v-model="formData.clausecode" size="small" disabled></el-input>
					</el-form-item>
					<el-form-item label="特约名称" required>
						<el-input v-model="formData.clausename" size="small"></el-input>
					</el-form-item>
					<el-form-item label="语种" required>
						<el-select v-model="formData.language" size="small">
							<el-option v-for="item in languageOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="有效标识" required>
						<el-select v-model="formData.validstatus" size="small">
							<el-option v-for="item in validOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="适用险种" required>
						<el-input v-model="riskCodesLabel" size="small" :title="riskCodesLabel" placeholder="适用险种代码" readonly @click.native="openRiskCodeSelectDialog()"></el-input>
					</el-form-item>
					<el-form-item label="适用默认带出险种">
						<el-input v-model="defaultRiskLabel" size="small" :title="defaultRiskLabel" placeholder="默认带出险种" readonly @click.native="openDefaultRiskSelectDialog()"></el-input>
					</el-form-item>
					<el-form-item label="适用默认带出险种方案">
						<el-input v-model="planLabel" size="small" :title="planLabel" placeholder="适用计划代码" readonly @click.native="openPlanSelectDialog()"></el-input>
					</el-form-item>
			  	</el-col>
			  	<el-col :span="12">
					<el-form-item label="适用分公司" required>
						<el-input v-model="comLabel" size="small"  :title="comLabel"  placeholder="适用分公司" readonly @click.native="openComSelectDialog()"></el-input>
					</el-form-item>
					<el-form-item label="适用默认带出分公司">
						<el-input v-model="defaultComLabel" size="small"  :title="defaultComLabel"  placeholder="默认带出分公司" readonly @click.native="openDefaultComSelectDialog()"></el-input>
					</el-form-item>
					<el-form-item label="适用代理人">
						<el-input v-model="agentLabel" size="small"  :title="agentLabel"  placeholder="适用代理人" readonly @click.native="openAgentSelectDialog()"></el-input>
					</el-form-item>
					<el-form-item label="内容是否可修改标志位">
						<el-select v-model="formData.contflag" size="small">
							<el-option v-for="item in contflagOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="是否可删除标志位">
						<el-select v-model="formData.delflag" size="small">
							<el-option v-for="item in delflagOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="初始化默认带出标志位">
						<el-select v-model="formData.iniflag" size="small">
							<el-option v-for="item in iniflagOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
			  	</el-col>
			  	<el-col :span="24">
				  	<el-form-item label="特约内容" required>
						<el-input type="textarea" v-model="formData.context" size="small"></el-input>
					</el-form-item>
			  	</el-col>
			  	<el-col :span="24">
				  	<el-form-item label="审核人备注">
						<el-input type="textarea" v-model="formData.remark" size="small"></el-input>
					</el-form-item>
			  	</el-col>
			  </el-row>
			</el-form>
			  <span slot="footer" class="dialog-footer">
				   <el-button @click="off">取 消</el-button>
				   <el-button type="success" @click="edit">修 改</el-button>
			  </span>
		</el-dialog>

		<RiskCodeSelect v-if="showRiskCodeSelectDialog"></RiskCodeSelect>
		<DefaultRiskSelect v-if="showDefaultRiskSelectDialog"></DefaultRiskSelect>
		<PlanSelect v-if="showPlanSelectDialog"></PlanSelect>
		<ComSelect v-if="showComSelectDialog"></ComSelect>
		<DefaultComSelect v-if="showDefaultComSelectDialog"></DefaultComSelect>
		<ClauseAgentSelect v-if="showAgentSelectDialog"></ClauseAgentSelect>
	</div>
</template>

<script>

import ClauseAPI from "../../../../js/production/clause/clauseAPI"
import RiskCodeSelect from "../../common/RiskCodeSelect";
import DefaultRiskSelect from "./DefaultRiskSelect";
import PlanSelect from "./PlanSelect";
import ComSelect from "../../common/ComSelect";
import DefaultComSelect from "./DefaultComSelect";
import ClauseAgentSelect from "./ClauseAgentSelect";

  export default {
	components : {
		RiskCodeSelect,
		DefaultRiskSelect,
		PlanSelect,
		ComSelect,
		DefaultComSelect,
		ClauseAgentSelect
 	 },
	data() {
		return {
			dialogVisible: true,
			formData: {
			    //prpdclause表
			    clausecode : '',
			    clausename : '',
			    language : '',
			    titleflag : '',
			    lineno : '',
			    context : '',
			    newclausecode : '',
			    validstatus : '',
			    flag : '',
			    comcode : '',
			    contflag : '',
			    delflag : '',
			    iniflag : '',
			    remark : '',
			    agentcode : '',

			    //prpdcoderisk表
			    riskcode: '',

			    //prpdriskclause表
			    defaultriskcode: '',
			    plancode: '',
			    defaultcomcode: '',

			},
			riskCodesLabel : '',
			defaultRiskLabel : '',
			planLabel : '',
			comLabel : '',
			defaultComLabel : '',
			agentLabel : '',
			arrayRiskcodes: [],
			arrayDefaultRisk: [],
			arrayPlancodes : [],
			arrayComcodes : [],
			arrayDefaultCom : [],
			arrayAgentcodes : [],
			dialog_margin_top : '2%',
			languageOptions: [
				{label : 'C-中文',value: 'C'},
				{label : 'E-英文',value: 'E'}
			],
			validOptions: [
				{label : '1-有效',value: '1'},
				{label : '0-无效',value: '0'}
			],
			contflagOptions: [
				{label : '1-内容不可修改',value: '1'},
				{label : '0-内容可修改',value: '0'}
			],
			delflagOptions: [
				{label : '1-不可删除',value: '1'},
				{label : '0-可删除',value: '0'}
			],
			iniflagOptions: [
				{label : '1-初始化不默认带出',value: '1'},
				{label : '0-初始化默认带出',value: '0'}
			],
			defaultRiskOptions : [],
			defaultComOptions : [],
			showRiskCodeSelectDialog : false,
			showDefaultRiskSelectDialog : false,
			showPlanSelectDialog : false,
			showComSelectDialog : false,
			showDefaultComSelectDialog : false,
			showAgentSelectDialog : false,
			originalRisk : '',				//判断适用险种是否变化
			originalDefaultRisk : '',			//判断适用默认带出险种是否变化
			originalCom : '',			//判断适用分公司是否变化
			originalDefaultCom : '',			//判断适用默认带出分公司是否变化
			loading : false,
		}
	},
	 watch : {
 	 	riskCodesLabel(newVal,oldVal) {
 	 		let risks = [];
 	 		for(let i = 0;i < this.arrayRiskcodes.length; i++) {
 	 			risks.push(this.arrayRiskcodes[i]);
 	 		}
 	 		this.defaultRiskOptions = risks;
 	 		this.formData.riskcode = newVal;
 	 	},
 	 	defaultRiskLabel(newVal,oldVal) {
 	 		this.formData.defaultriskcode = newVal;
 	 	},
 	 	planLabel(newVal,oldVal) {
 	 		this.formData.plancode = newVal;
 	 	},
 	 	comLabel(newVal,oldVal) {
 	 		let coms = [];
 	 		for(let i = 0;i < this.arrayComcodes.length; i++) {
 	 			coms.push(this.arrayComcodes[i]);
 	 		}
 	 		this.defaultComOptions = coms;
 	 		this.formData.comcode = newVal;
 	 	},
 	 	defaultComLabel(newVal,oldVal) {
 	 		this.formData.defaultcomcode = newVal;
 	 	},
 	 	agentLabel(newVal,oldVal) {
 	 		this.formData.agentcode = newVal;
 	 	}
 	 },
	mounted() {
		let temp = Object.assign({}, this.$parent.selectedRows[0]);
		temp.createdate=undefined;
		temp.updatedate=undefined;
		this.formData= temp;
		ClauseAPI.queryCodeRiskByClause(this);
		this.agentLabel = temp.agentcode;
		this.comLabel = temp.comcode;

		if(typeof(temp.comcode) !=='undefined' && temp.comcode !== null && temp.comcode !== '') {
			this.arrayComcodes = temp.comcode.split(',');
		}
		if(typeof(temp.agentcode) !=='undefined' && temp.agentcode !== null && temp.agentcode !== '') {
			this.arrayAgentcodes = temp.agentcode.split(',');
		}
		let vm = this;
		//适用险种值改变时，清空适用默认带出险种及适用默认带出险种方案
		setInterval(function(){
	                    if(vm.riskCodesLabel !== vm.originalRisk) {
	                    	 // vm.defaultRiskLabel = '';
	                    	 // vm.planLabel = '';
	                    	 vm.arrayDefaultRisk = [];
 	 		 // vm.arrayPlancodes = [];
	                    	 vm.originalRisk = vm.riskCodesLabel;

	                    	 ////////////////////////////////////////////////////////////////////////////////////
 	 		//重新组装默认险种Label
 	 		if (vm.defaultRiskOptions.length >= 0 && vm.formData.defaultriskcode !== '') {   
	 	 		let defRisks = vm.formData.defaultriskcode.split(",");
	 	 		let tmpDefRiskLabel = '';
	 	 		for (let i in defRisks) {
	 	 			for(let j in vm.defaultRiskOptions) {
	 	 				if (defRisks[i] === vm.defaultRiskOptions[j]) {
	 	 					tmpDefRiskLabel +=  defRisks[i] + ',';
	 	 					vm.arrayDefaultRisk.push(vm.defaultRiskOptions[j]);
	 	 					break;
	 	 				}
	 	 			}
	 	 		}
	 	 		vm.defaultRiskLabel = tmpDefRiskLabel.substring(0, tmpDefRiskLabel.length - 1);
 	 		}
 	 		////////////////////////////////////////////////////////////////////////////////////

	                    }
	            },500);
		//适用默认带出险种值改变时，清空适用默认带出险种方案和默认带出分公司
		setInterval(function(){
	                    if(vm.defaultRiskLabel !== vm.originalDefaultRisk) {
	                    	 // vm.planLabel = '';
 	 		 vm.arrayPlancodes = [];
 	 		 // vm.defaultComLabel = '';
 	 		 // vm.arrayDefaultCom = [];
	                    	 vm.originalDefaultRisk = vm.defaultRiskLabel;

	                    	 ////////////////////////////////////////////////////////////////////////////////////
 	 		//重新组装默认方案Label
 	 		if (vm.formData.defaultriskcode !== '' && vm.formData.plancode !== '') {   
 	 			let planOptions = ClauseAPI.queryPlanOptions(vm);
	 	 		let defPlans = vm.formData.plancode.split(",");
	 	 		let tmpDefPlanLabel = '';
	 	 		for (let i in defPlans) {
	 	 			for(let j in planOptions) {
	 	 				if (defPlans[i] === planOptions[j].plancode) {
	 	 					tmpDefPlanLabel +=  defPlans[i] + ',';
	 	 					vm.arrayPlancodes.push(planOptions[j].plancode);
	 	 					break;
	 	 				}
	 	 			}
	 	 		}
	 	 		vm.planLabel = tmpDefPlanLabel.substring(0, tmpDefPlanLabel.length - 1);
 	 		}
 	 		if (vm.defaultRiskLabel === '') {
 	 			vm.planLabel = '';
 	 		}
	                    }
	            },500);
		//适用分公司值改变时，清空适用默认带出分公司及适用代理人
		setInterval(function(){
	                    if(vm.comLabel !== vm.originalCom) {
	                    	 // vm.defaultComLabel = '',
			 // vm.agentLabel = '',
	                    	 vm.arrayDefaultCom = [];
 	 		 // vm.arrayAgentcodes = [];
	                    	 vm.originalCom = vm.comLabel;

	                    	 ////////////////////////////////////////////////////////////////////////////////////
 	 		//重新组装默认公司Label
 	 		if (vm.defaultComOptions.length >= 0 && vm.formData.defaultcomcode !== '') {   
	 	 		let defComs = vm.formData.defaultcomcode.split(",");
	 	 		let tmpDefComLabel = '';
	 	 		for (let i in defComs) {
	 	 			for(let j in vm.defaultComOptions) {
	 	 				if (defComs[i] === vm.defaultComOptions[j]) {
	 	 					tmpDefComLabel +=  defComs[i] + ',';
	 	 					vm.arrayDefaultCom.push(vm.defaultComOptions[j]);
	 	 					break;
	 	 				}
	 	 			}
	 	 		}
	 	 		vm.defaultComLabel = tmpDefComLabel.substring(0, tmpDefComLabel.length - 1);
 	 		}
 	 		////////////////////////////////////////////////////////////////////////////////////

 	 		////////////////////////////////////////////////////////////////////////////////////
 	 		//重新组装代理人Label
 	 		if (vm.defaultComOptions.length >= 0 && vm.formData.agentcode !== '' && vm.formData.comcode.indexOf('ALL') < 0) { 
 	 			vm.arrayAgentcodes = [];  
 	 			let agents = AgentAPI.queryAgentByChange(vm);
	 	 		let agentCodes = vm.formData.agentcode.split(",");
	 	 		let tmpAgentLabel = '';
	 	 		for (let i in agentCodes) {
	 	 			for(let j in agents) {
	 	 				if (agentCodes[i] === agents[j].agentcode) {
	 	 					tmpAgentLabel +=  agents[j].agentcode + ',';
	 	 					vm.arrayAgentcodes.push(agents[j].agentcode);
	 	 					break;
	 	 				}
	 	 			}
	 	 		}
	 	 		vm.agentLabel = tmpAgentLabel.substring(0, tmpAgentLabel.length - 1);
 	 		}
 	 		////////////////////////////////////////////////////////////////////////////////////

	                    }
	            },500);
		//适用默认带出分公司值改变时，清空及适用代理人
		// setInterval(function(){
	     //               if(vm.defaultComLabel !== vm.originalDefaultCom) {
			 // vm.agentLabel = '',
 	 		 // vm.arrayAgentcodes = [];
	     //                	 vm.originalDefaultCom = vm.defaultComLabel;
	     //                }
	     //        },500);
	},
	methods: {
		edit() {
			ClauseAPI.edit(this);
		},
		off() {
			this.$parent.showEditDialog = false;
		},
		openRiskCodeSelectDialog() {
			ClauseAPI.openRiskCodeSelectDialog(this);
		},
		openDefaultRiskSelectDialog() {
			ClauseAPI.openDefaultRiskSelectDialog(this);
		},
		openPlanSelectDialog() {
			ClauseAPI.openPlanSelectDialog(this);
		},
		openComSelectDialog() {
			ClauseAPI.openComSelectDialog(this);
		},
		openDefaultComSelectDialog() {
			ClauseAPI.openDefaultComSelectDialog(this);
		},
		openAgentSelectDialog() {
			ClauseAPI.openAgentSelectDialog(this);
		},
	}
  }
</script>
